//
//  ViewController.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/3.
//

#import "ViewController.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "AppDelegate.h"
#import "SDHSportsStartModel.h"
#import "MJExtension.h"

@interface ViewController ()

@property (nonatomic, strong) UILabel *lbCountdown;
@property (nonatomic, assign) NSInteger countdownNum;
@property (nonatomic, strong) SDHSportsStartModel *model;
@property (nonatomic, strong) UIButton *btnRetry;
@property (nonatomic, strong) UIButton *btnSkip;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _countdownNum = 6;
    
    [self initUI];
    [self loadData];
    [self countdown];
}

- (void)initUI {
    UIImageView *imgView = [UIImageView new];
    imgView.image = [UIImage imageNamed:@"launch"];
    [self.view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
    
    UILabel *lbCountdown = [UILabel new];
    lbCountdown.font = [UIFont systemFontOfSize:20];
    lbCountdown.textColor = [UIColor whiteColor];
    lbCountdown.backgroundColor = [[UIColor blackColor] addAlpha:0.5];
    lbCountdown.textAlignment = NSTextAlignmentCenter;
    lbCountdown.clipsToBounds = YES;
    lbCountdown.layer.cornerRadius = 15;
    [self.view addSubview:lbCountdown];
    lbCountdown.text = @"nhảy qua 5";
    self.lbCountdown = lbCountdown;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skip:)];
    [self.lbCountdown addGestureRecognizer:tap];
    self.lbCountdown.userInteractionEnabled = YES;
    
    [lbCountdown mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(50);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *btnSkip = [UIButton new];
    [btnSkip setTitle:@"Skip" forState:UIControlStateNormal];
    btnSkip.backgroundColor = [UIColor redColor];
    [self.view addSubview:btnSkip];
    btnSkip.hidden = YES;
    self.btnSkip = btnSkip;
    [btnSkip addTarget:self action:@selector(skip:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnSkip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-150);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(60);
    }];
    
    UIButton *btnRetry = [UIButton new];
    [btnRetry setTitle:@"Retry" forState:UIControlStateNormal];
    btnRetry.backgroundColor = [UIColor redColor];
    [self.view addSubview:btnRetry];
    self.btnRetry = btnRetry;
    btnRetry.hidden = YES;
    [btnRetry addTarget:self action:@selector(retry:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnRetry mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-150);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(60);
    }];
}

- (void)retry:(UIButton *)btn {
    self.btnRetry.enabled = NO;
    [MBProgressHUD showLoading];
    [self loadData];
}

- (void)skip:(UIButton *)btn {
    self.lbCountdown.hidden = YES;
    self.btnSkip.hidden = YES;
}

- (void)networkError {
    [MBProgressHUD showError:@"web không hợp lệ"];
    self.btnRetry.hidden = NO;
    self.btnRetry.enabled = YES;
    self.lbCountdown.hidden = YES;
}

- (void)countdown {
    self.lbCountdown.text = [NSString stringWithFormat:@"nhảy qua %ld", _countdownNum];
    _countdownNum --;
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (weakSelf.countdownNum > 0) {
            [weakSelf countdown];
        } else {
            [weakSelf skip:nil];
        }
    });
}

- (void)enterApp {
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if ([self.model.state intValue] == 0) {
        [appDelegate enterBaseView:self.model];
    } else {
        [appDelegate enterMainView];
    }
}

- (void)parseData:(NSDictionary *)dic {
    NSDictionary *body = [dic objectForKey:@"data"];
    NSLog(@"request body: %@", body);
    if ([body isKindOfClass:[NSDictionary class]]) {
        self.model = [SDHSportsStartModel mj_objectWithKeyValues:body];
        [self enterApp];
    }
}

- (void)loadData {
    SDHSportsRequest *req = [[SDHSportsRequest alloc] init];
    req.requestUrl = @"http://jisuwjji.com/index.php/Admin/interface/get";
    req.requestPara =  @{@"appid":kAppid};
    
    __weak typeof(self) weakSelf = self;
    [[SDHSportsNetRequest sharedRequest] postRequest:req returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf requestAgain];
    } failureBlock:^{
        
    }];
}

- (void)requestAgain {
    SDHSportsRequest *req = [[SDHSportsRequest alloc] init];
    req.requestUrl = @"http://sssuijdk.com/index.php/Admin/interface/get";
    req.requestPara =  @{@"appid":kAppid};
    
    __weak typeof(self) weakSelf = self;
    [[SDHSportsNetRequest sharedRequest] postRequest:req returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf networkError];
    } failureBlock:^{
        
    }];
}

@end
